from flask import Blueprint, request, render_template, url_for
import flask
from mostone import mostone

mostone_app = Blueprint("mostone_app", __name__)

@mostone_app.route("/mostone/manifest/<version>")
@mostone_app.route("/mostone/manifest/<version>/<name>")
def generate_manifest(version=None, name=None):
    if version is None:
        return "请带上版本号"
    if name is None:
        name = "miyo"
    most = mostone.Mostone()
    if name == "miyo":
        most.generate_miyo_manifest(version=version)
    else:
        most.generate_luka_manifest(version=version)
    return "success"

KY_MAC_DEBUG_DOMAIN = "http://192.168.199.80:9998/Desktop/LocalIpas/"
KY_MAC_RELEASE_DOMAIN = "http://192.168.199.80:9998/Documents/ipas/"

@mostone_app.route("/mostone/ipas/", methods=["get"])
def get_ipas_list():
    mos = mostone.Mostone()
    #历史包
    allversions = mos.get_all_versions()
    miyo = allversions["MIYO"]
    luka = allversions["LUKA"]
    mimi = allversions["MIMI"]
    mowang = allversions["MOSTONE"]
    miyo_info = {
        "versions": miyo,
        "lastversion": miyo[0],
    }
    luka_info = {
        "versions": luka,
        "lastversion": luka[0],
    }
    mimi_info = {
        "versions": mimi,
        "lastversion": mimi[0],
    }
    mowang_info = {
        "versions": mowang,
        "lastversion": mowang[0]
    }
    #打包机
    sys_allversions = mos.get_shiyanshi_all_versions()
    sys_miyo = sys_allversions["MIYO"]
    sys_luka = sys_allversions["LUKA"]
    sys_miyo_info = {
        "versions": sys_miyo,
        "lastversion": sys_miyo[0],
    }
    sys_luka_info = {
        "versions": sys_luka,
        "lastversion": sys_luka[0],
    }
    #个人本机
    self_allversions = {
        "luka": {
            "version": "最新",
            "date": "未知",
            "debug_url": "itms-services://?action=download-manifest&url=https://app-distribution-1310744949.cos.ap-shanghai.myqcloud.com/lxf/mopro_manifest_debug_m2.plist",
            "release_url": "itms-services://?action=download-manifest&url=https://app-distribution-1310744949.cos.ap-shanghai.myqcloud.com/lxf/mopro_manifest_release_m2.plist",
            "xcode15_url": "itms-services://?action=download-manifest&url="
        },
        "mowang": {
            "version": "最新",
            "date": "未知",
            "debug_url": "itms-services://?action=download-manifest&url=https://app-distribution-1310744949.cos.ap-shanghai.myqcloud.com/lxf/mw_manifest_debug_m2.plist",
            "release_url": "itms-services://?action=download-manifest&url=https://app-distribution-1310744949.cos.ap-shanghai.myqcloud.com/lxf/mw_manifest_release_m2.plist",
            "xcode15_url": "itms-services://?action=download-manifest&url="
        },
        "jrzx": {
            "version": "最新",
            "date": "未知",
            "debug_url": "itms-services://?action=download-manifest&url=https://app-distribution-1310744949.cos.ap-shanghai.myqcloud.com/cfwifine/jrzx_manifest_debug_m2.plist",
            "release_url": "itms-services://?action=download-manifest&url=https://app-distribution-1310744949.cos.ap-shanghai.myqcloud.com/cfwifine/jrzx_manifest_release_m2.plist",
        },
        "xqt": {
            "version": "最新",
            "date": "未知",
            "debug_url": "itms-services://?action=download-manifest&url=https://app-distribution-1310744949.cos.ap-shanghai.myqcloud.com/cfwifine/xqt_manifest_debug_m2.plist",
            "release_url": "itms-services://?action=download-manifest&url=https://app-distribution-1310744949.cos.ap-shanghai.myqcloud.com/cfwifine/xqt_manifest_release_m2.plist",
        },
        "jx": {
            "version": "最新",
            "date": "未知",
            "debug_url": "itms-services://?action=download-manifest&url=https://app-distribution-1310744949.cos.ap-shanghai.myqcloud.com/cfwifine/jx_manifest_debug_m2.plist",
            "release_url": "itms-services://?action=download-manifest&url=https://app-distribution-1310744949.cos.ap-shanghai.myqcloud.com/cfwifine/jx_manifest_release_m2.plist",
        },
        "cx": {
            "version": "最新",
            "date": "未知",
            "debug_url": "itms-services://?action=download-manifest&url=https://app-distribution-1310744949.cos.ap-shanghai.myqcloud.com/cfwifine/cx_manifest_debug_m2.plist",
            "release_url": "itms-services://?action=download-manifest&url=https://app-distribution-1310744949.cos.ap-shanghai.myqcloud.com/cfwifine/cx_manifest_release_m2.plist",
        }
    }

    versioninfo = {
        "miyo": miyo_info,
        "luka": luka_info,
        "mimi": mimi_info,
        "mowang": mowang_info,
        "miyo_more_url": request.host_url + flask.url_for("mostone_app.get_ipas_more", name="miyo"),
        "luka_more_url": request.host_url + flask.url_for("mostone_app.get_ipas_more", name="luka"),
        "mimi_more_url": request.host_url + flask.url_for("mostone_app.get_ipas_more", name="mimi"),
        "mowang_more_url": request.host_url + flask.url_for("mostone_app.get_ipas_more", name="mowang"),
        "sys_miyo": sys_miyo_info,
        "sys_luka": sys_luka_info,
        "self_allversions": self_allversions
    }
    return render_template("mostone.html", versioninfo=versioninfo)

@mostone_app.route("/mostone/ipas/more/")
@mostone_app.route("/mostone/ipas/more/<name>")
def get_ipas_more(name=None):
    if name is None:
        name = "miyo"
    mos = mostone.Mostone()
    allversions = mos.get_all_versions()
    if name == "miyo":
        versions = allversions["MIYO"]
    elif name == "luka":
        versions = allversions["LUKA"]
    elif name == "mimi":
        versions = allversions["MIMI"]
    else:
        versions = allversions["MOSTONE"]
    versioninfo = {
        "ismore": True,
        "versions": versions
    }
    print(allversions)
    return render_template("mostone.html", versioninfo=versioninfo)

######################################################################################################################
@mostone_app.route("/mostone/facility/")
def facility_page():
    return render_template("mwfacility.html")
    pass